<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Faqs_model extends Model
{
	private $_host;
	private $_dbname;
	private $_username;
	private $_password;
	private $_port;

	function Faqs_model()
	{
		parent::Model();
		
		$host = $this->db->hostname;
		$arrHost = explode(':',$host);
		
		$this->_host = trim($arrHost[0]);
		$this->_dbname = $this->db->database;
		$this->_username = $this->db->username;
		$this->_password = $this->db->password;
		$this->_port = $this->db->port;
	}
	
	function getall_categories()
	{
		$data = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_faq_category_getall();") or die(mysqli_error($iconn));

		while($row = mysqli_fetch_object($result))
		{
			$data[] = $row;
		}
		
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function get_topics($catid=0, $parent_id=0, $page = 1, $limit = 20)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_faq_topic_getall($catid,$parent_id,$page,$limit);") or die(mysqli_error($iconn));
		$data = array();
		$data['items'] = array();
		while($row = mysqli_fetch_object($result))
		{
			$data['items'][] = $row;
		}
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$row = mysqli_fetch_object($result);
		$data['pages'] = (int)$row->totalpage;
		
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function getTopicVote($id)
	{
		$data = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_faq_vote_process($id);") or die(mysqli_error($iconn));

		while($row = mysqli_fetch_object($result))
		{
			$data[] = $row;
		}
		
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function get_topic_info($id)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_topic_faq_get_info($id);") or die(mysqli_error($iconn));
		$row = mysqli_fetch_object($result);
		
		mysqli_close($iconn);
		return $row;
	}
	
	function update_topic($id, $content, $catid, $parentid)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_faq_topic_update($id, '$content', $catid, $parentid);") or die(mysqli_error($iconn));

		mysqli_close($iconn);
	}
	
	function insert_topic($content, $catid=0, $parentid=0)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_faq_topic_insert('$content',$catid,$parentid);") or die(mysqli_error($iconn));

		mysqli_close($iconn);
	}
	
	function updateCategoryName($id, $text)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		mysqli_query($iconn, "CALL selfserving_faq_update_category_name($id,'$text');") or die(mysqli_error($iconn));

		mysqli_close($iconn);
	}
	
	function insert_category($name, $parentid=0)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_faq_category_insert('$name',$parentid);") or die(mysqli_error($iconn));

		mysqli_close($iconn);
	}
}